// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.commons.ui.swt.viewers;

import java.text.FieldPosition;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.dengues.commons.i18n.Messages;
import org.dengues.commons.ui.swt.calendar.SWTCalendarDialog;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.DialogCellEditor;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class DateCellEditor extends DialogCellEditor {

    private String pattern;

    /**
     * Qiang.Zhang.Adolf@gmail.com DateCellEditor constructor comment.
     * 
     * @param parent
     */
    public DateCellEditor(Composite parent, String pattern) {
        super(parent);
        this.pattern = pattern;
    }

    /**
     * qiang.zhang DateCellEditor constructor comment.
     * 
     * @param parent
     * @param style
     */
    public DateCellEditor(Composite parent, int style) {
        super(parent, style);
    }

    /*
     * (non-Java)
     * 
     * @see org.eclipse.jface.viewers.DialogCellEditor#openDialogBox(org.eclipse.swt.widgets.Control)
     */
    @Override
    protected Object openDialogBox(Control cellEditorWindow) {
        SWTCalendarDialog d = new SWTCalendarDialog(cellEditorWindow.getShell(), new Date(), Messages.getString("DateCellEditor.title")); //$NON-NLS-1$
        int res = d.open();
        if (res == Dialog.OK) {
            String dateString = getDateString(d.getCalendar().getTime());
            setModelDateString(dateString);
            return dateString;
        } else {
            return getValue();
        }
    }

    public String getDateString(Date calendar) {
        StringBuffer result = new StringBuffer();

        if (pattern == null || "".equals(pattern)) { //$NON-NLS-1$
            pattern = "yyyy-MM-dd HH:mm:ss"; //$NON-NLS-1$
        }

        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        sdf.format(calendar, result, new FieldPosition(0));
        return result.toString();
    }

    public void setModelDateString(String dateString) {

    }
}
